package com.android.photoalbum.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListPopupWindow;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.photoalbum.PreViewPhotoActivity;
import com.android.photoalbum.R;
import com.android.photoalbum.bean.AlbumFolderBean;
import com.android.photoalbum.bean.AlbumPhotoInfoBean;
import com.android.photoalbum.ui.adapter.AlbumShowRvAdapter;
import com.android.photoalbum.utils.ImageVideoLoader;
import com.android.photoalbum.weiget.AlbumItemHeaderDecoration;
import com.android.photoalbum.weiget.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class PhotoFragment extends Fragment implements AlbumLoadDataCallback, AlbumShowRvAdapter.OnRecyclerViewItemClickListener {

    private RecyclerView mRecyclerView;
    private AlbumShowRvAdapter mAlbumShowRvAdapter;
    private static final String maxSelectExtra = "maxSelectExtra";
    private int mMaxSelectCount = 1;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_photo, container, false);
        initView(root);
        initData();
        initListener();
        return root;
    }

    private void initListener() {

    }

    private void initData() {
        mAlbumShowRvAdapter = new AlbumShowRvAdapter(requireContext(), mMaxSelectCount);
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 3);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(requireContext(), mAlbumShowRvAdapter));
        mRecyclerView.addItemDecoration(new AlbumItemHeaderDecoration(requireContext(), mAlbumShowRvAdapter));
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mAlbumShowRvAdapter.getItemViewType(position) == AlbumShowRvAdapter.HEAD_TYPE) {
                    return gridLayoutManager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
        mRecyclerView.setAdapter(mAlbumShowRvAdapter);
        mAlbumShowRvAdapter.setOnItemClickListener(this);
        requireActivity().getLoaderManager().initLoader(1, null, new ImageVideoLoader(requireContext(), this));
    }

    private void initView(View root) {
        mRecyclerView = root.findViewById(R.id.recyclerView);
    }


    @Override
    public void onItemClick(AlbumPhotoInfoBean data, int position) {
        List<Integer> headPositionList = mAlbumShowRvAdapter.getHeadPositionList();
        int count = 0;
        for (int i = 0; i < headPositionList.size(); i++) {
            if (position > headPositionList.get(i)) {
                count++;
            } else {
                break;
            }
        }
        startPreViewActivity(position - count, mAlbumShowRvAdapter.getAllDataNoHead());
    }

    private void startPreViewActivity(int position, List<AlbumPhotoInfoBean> allData) {
        PreViewPhotoActivity.startActivity(requireActivity(), position, mMaxSelectCount, allData, mAlbumShowRvAdapter.getAllSelectData(), 100);
    }

    @Override
    public void onData(ArrayList<AlbumFolderBean> list) {
        mAlbumShowRvAdapter.updateAdapterList(list.get(0).getAlbumFolderList(), list.get(0).getHeadPositionList());
    }
}